#include "EmitterSceneSettings.h"

#include <fstream>
#include <streambuf>
#include <string>

#include <rapidjson/document.h>	// rapidjson's DOM-style API
#include <rapidjson/prettywriter.h>	// for stringify JSON
#include <rapidjson/filestream.h>	// wrapper of C stream for prettywriter as output

namespace emitterScene {
	Settings::Settings(const std::wstring& settingsFile) 
		: mNumEntities(0)
		, mDispatchGroupsX(0)
		, mDispatchGroupsY(0)
		, mDispatchGroupsZ(0)
	{
		// Read file and save its content into a char array
		std::ifstream file;
		file.open(settingsFile, std::ifstream::in);
		assert(file.is_open());
		const std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
		file.close();

		// Parse json file
		rapidjson::Document document;
		if (document.Parse<0>(str.c_str()).HasParseError()) {
			assert(false);
			return;
		}

		// Fill data with parsed data
		assert(document.HasMember("numEntities"));
		assert(document["numEntities"].IsInt());
		mNumEntities = document["numEntities"].GetInt();

		assert(document.HasMember("dispatchGroupsX"));
		assert(document["dispatchGroupsX"].IsInt());
		mDispatchGroupsX = document["dispatchGroupsX"].GetInt();

		assert(document.HasMember("dispatchGroupsY"));
		assert(document["dispatchGroupsY"].IsInt());
		mDispatchGroupsY = document["dispatchGroupsY"].GetInt();

		assert(document.HasMember("dispatchGroupsZ"));
		assert(document["dispatchGroupsZ"].IsInt());
		mDispatchGroupsZ = document["dispatchGroupsZ"].GetInt();
	}
}